<template>
  <div class="app-container">
    <el-form :inline="true" :model="studyType" class="demo-form-inline">
      <el-form-item style="margin-left: 32%;" label="课程类型名称">
        <el-input v-model="studyType.typeName" placeholder="请输入课程类型名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onClear">清空</el-button>
      </el-form-item>
    </el-form>
  <el-button type="primary" @click="linkToAdd">新增课程类型</el-button>
  <el-button type="danger" v-show="showDelBtn" @click="delStudyTypes">删除</el-button>
  <el-table
      v-loading="listLoading"
      :data="studyTypes"
      element-loading-text="Loading"
      highlight-current-row
      @selection-change="handleSelectionChange">
  >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="143">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="课程类型" width="350" align="center">
        <template slot-scope="scope">
          {{ scope.row.typeName }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>
            {{ scope.row.createTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="320" align="center">
        <template slot-scope="scope">
          {{ scope.row.orderNum }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="325">
        <template slot-scope="scope">
          <el-button title="编辑" type="primary" icon="el-icon-edit" circle @click="linkToEdit(scope.row.id)" />
          <el-button title="删除" type="danger" icon="el-icon-delete" circle @click="delStudyType(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <nav style="text-align: center">
      <el-pagination
      background
      @current-change="handlePageChange"
      :current-page="pageNo"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
    </nav>
  </div>
  
</template>

<script>
import { getStudyTypesApi, delStudyTypeApi, delStudyTypesApi } from '@/api/studyType'
export default {
  data() {
    return {
      imgPath:process.env.VUE_APP_BASE_API+'/',
      studyType:{},
      pageNo: 1,
      pageSize: 10,
      studyTypes: [],
      listLoading: true,
      delStudyTypeArray:[],
      showDelBtn:false,
      total: 0
    }
  },
  created() {
    this.getStudyTypes(this.pageNo, this.pageSize, this.studyType)
  },
  methods: {
    handlePageChange(currentPage) {
      this.pageNo = currentPage;
      this.getStudyTypes(this.pageNo, this.pageSize, this.studyType)
    },
    handleSelectionChange(val) {
      if(val.length>0){
        this.showDelBtn=true
      }else{
        this.showDelBtn=false
      }
      this.delStudyTypeArray = val
    },
    delStudyTypes(){
      this.listLoading = true
      delStudyTypesApi(this.delStudyTypeArray)
        .then(response=>{
          this.getStudyTypes(this.pageNo, this.pageSize)
          this.listLoading = false
        })
    },
    linkToAdd() {
      this.$router.push('/content/studyType/add')
    },
    onSearch(){
      this.pageNo = 1
      this.getStudyTypes(this.pageNo, this.pageSize, this.studyType)
    },
    onClear() {
      this.studyType={}
      this.getStudyTypes(this.pageNo, this.pageSize, this.studyType)
    },
    getStudyTypes(pageNo, pageSize, studyType) {
      this.listLoading = true
      getStudyTypesApi(pageNo, pageSize, this.studyType)
        .then(response => {
          this.studyTypes = response.data.list
          this.pageNo = response.data.pageNum
          this.total = response.data.total
          this.listLoading = false
        })
    },
    linkToEdit(id) {
      this.$router.push({name:'editStudyType', params:{id:id}})
    },
    delStudyType(id) {
      this.listLoading = true
      delStudyTypeApi(id).then(response => {
        this.getStudyTypes(this.pageNo, this.pageSize)
        this.listLoading = false
      })
    }
  }
}
</script>

